In [1]:
# We start by importing MPRester, which is available from the root import of pymatgen.
from pymatgen import MPRester
from pprint import pprint
# Initializing MPRester. Note that you can call MPRester. MPRester looks for the API key in two places:
# - Supplying it directly as an __init__ arg.
# - Setting the "MAPI_KEY" environment variable.
# Please obtain your API key at https://www.materialsproject.org/dashboard
m = MPRester()
Many methods in MPRester supports the extremely simple yet powerful query syntax for materials. There are three kinds of queries:
The MPRester automatically detects what kind of query is being made. Also, for formulas and chemical systems, wildcards are supported with a *. That means *2O will yield a list of the following formula results:
B2O, Xe2O, Li2O ...
In [2]:
#The following query returns all structures in the Materials Project with formula "Fe2O3"
pprint(m.get_data("Li2O", prop="structure"))
In [3]:
# These query returns the chemical formula and material id of all Materials with formula of form "*3O4".
# The material_id is always returned with any use of get_data.
pprint(m.get_data("*3O4", prop="pretty_formula"))
In [4]:
# Getting a DOS object and plotting it. Bandstructures are similar.
dos = m.get_dos_by_material_id("mp-19017")
bs = m.get_bandstructure_by_material_id("mp-19017")
from pymatgen.electronic_structure.plotter import DosPlotter, BSPlotter
%matplotlib inline
dos_plotter = DosPlotter()
dos_plotter.add_dos_dict(dos.get_spd_dos())
dos_plotter.show()
bs_plotter = BSPlotter(bs)
bs_plotter.show()
The query() method essentially works almost like a raw MongoDB query on the Materials Project database. With it, you can perform extremely sophisticated queries to obtain large and customized quantities of materials data easily. The way to use query is
query(criteria, properties)
The criteria argument can either be a simple string similar to the powerful wildcard based formula and chemical system search described above, or a full MongoDB query dict with all the features of the Mongo query syntax.
In [5]:
# Get material ids for everything in the Materials Project database
data = m.query(criteria={}, properties=["task_id"])
In [6]:
# Get the energy for materials with material_ids "mp-1234" and "mp-2345".
data = m.query(criteria={"task_id": {"$in": ["mp-1234", "mp-1"]}}, properties=["final_energy"])
print data
In [7]:
# Get the spacegroup symbol for all materials with formula Li2O.
data = m.query(criteria={"pretty_formula": "Li2O"}, properties=["spacegroup.symbol"])
print data
In [8]:
# Get the ICSD of all compounds containing either K, Li or Na with O.
data = m.query(criteria={"elements": {"$in": ["K", "Li", "Na"], "$all": ["O"]}, "nelements": 2},
properties=["icsd_id", "pretty_formula", "spacegroup.symbol"])
pprint(data)
If you decide not to install pymatgen, you can still make use of the Materials API by calling the relevant URLs directly. Here, we will demonstrate how you can do so using the requests library, though any http library should work similarly. All the queries demonstrated here are similar to the above queries.
In [9]:
import requests
import os
import json
In [10]:
r = requests.get("https://www.materialsproject.org/rest/v2/materials/Li2O/vasp/final_structure",
headers={"X-API-KEY": os.environ["MAPI_KEY"]})
content = r.json() # a dict
In [11]:
r = requests.get("https://www.materialsproject.org/rest/v2/materials/*3O4/vasp/pretty_formula",
headers={"X-API-KEY": os.environ["MAPI_KEY"]})
content = r.json() # a dict
pprint(content["response"])
Note that we cannot demonstrate DOS nad Bandstructure plotting here, since those rely on pymatgen's high level plotting utilities for these objects. But you can of course query for the DOS and Bandstructure data and implement your own customized plotting in your favorite graphing utility.
In [12]:
data = {
"criteria": {
"elements": {"$in": ["Li", "Na", "K"], "$all": ["O"]},
"nelements": 2,
},
"properties": [
"icsd_id",
"pretty_formula",
"spacegroup.symbol"
]
}
r = requests.post('https://materialsproject.org/rest/v2/query',
headers={'X-API-KEY': os.environ["MAPI_KEY"]},
data={k: json.dumps(v) for k,v in data.iteritems()})
content = r.json() # a dict
pprint(content["response"])
In [ ]: